package maildao

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"xr-game-server/core/cache"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/constants/db"
	"xr-game-server/entity"
)

var roleMailCache *cache.CacheMgr

func initRoleMail() {
	roleMailCache = cache.NewCacheMgr()
}

func GetRoleMailBy(roleId uint64) []*entity.RoleMail {
	ret := roleMailCache.GetData(roleId, func(ctx context.Context) (value interface{}, err error) {
		dbs := make([]*entity.RoleMail, common.Zero)
		g.Model(string(entity.TbRoleMail)).Unscoped().Where(g.Map{
			string(entity.RoleMailRoleId): roleId,
			string(db.IsDeletedName):      false,
		}).Scan(&dbs)
		return dbs, nil
	})
	return ret.([]*entity.RoleMail)
}

func AddRoleMail(roleMail *entity.RoleMail) {
	db := GetRoleMailBy(roleMail.RoleId)
	db = append([]*entity.RoleMail{roleMail}, db...)
	roleMailCache.FlushCache(roleMail.RoleId, db)
}

func DelRoleMail(mailId uint64) {

}

func FlushRoleMail(roleId uint64, data []*entity.RoleMail) {
	roleMailCache.FlushCache(roleId, data)
}
